package com.snail.cloud.exception;

import com.snail.cloud.model.ResultCodeEnum;
import com.snail.cloud.model.ResultData;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author MR.LHL
 * @date 2024/2/26
 */

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(UsernameNotFoundException.class)
    public Object exceptionHandler(Exception e) {
        ResultData resultData = ResultData.error(ResultCodeEnum.USERNAME_NOT_EXISTS, e.getMessage());
        return resultData;
    }

    @ExceptionHandler(BadCredentialsException.class)
    public Object BadCredentialsExceptionHandler(Exception e) {
        ResultData resultData = ResultData.error(ResultCodeEnum.USERNAME_PASSWORD_ERROR, e.getMessage());
        return resultData;
    }

    @ExceptionHandler(RuntimeException.class)
    public Object RuntimeExceptionHandler(Exception e) {
        ResultData resultData = ResultData.error(ResultCodeEnum.FAIL, e.getMessage());
        return resultData;
    }

}
